#if !defined(MSG_WEB_VIEW_H__)
#    define MSG_WEB_VIEW_H__

#    pragma once

#    include <QDomNode>
#    include <QList>
#    include <QObject>
#    include <QStringList>
#    include <QWebEngineView>
#    include <data/Message.pb.h>
#    include <data/UserInfo.pb.h>
using namespace proto;
#    include "TalkUserInfo.hpp"

class MsgHtmlObj : public QObject {
    Q_OBJECT;

    Q_PROPERTY(QString msgLHtmlTmpl MEMBER m_msgLHtmlTmpl NOTIFY signalMsgHtml);    // 别人发的信息 模板
    Q_PROPERTY(QString msgRHtmlTmpl MEMBER m_msgRHtmlTmpl NOTIFY signalMsgHtml);    // 我发的信息 模板
    Q_PROPERTY(QString fileLHtmlTmpl MEMBER m_fileLHtmlTmpl NOTIFY signalMsgHtml);  // 别人发的文件 模板
    Q_PROPERTY(QString fileRHtmlTmpl MEMBER m_fileRHtmlTmpl NOTIFY signalMsgHtml);  // 我发的文件 模板
public:
    MsgHtmlObj(QObject* parent);
    ~MsgHtmlObj();

private:
    void    initHtmlTmpl();  // 初始化聊天网页
    QString getMsgTmplHtml(const QString& code);

public slots:
    void downloadFile(QString fileurl);

signals:
    void signalMsgHtml(const QString& html);

private:
    QString m_msgLHtmlTmpl;   // 别人发的信息 模板
    QString m_msgRHtmlTmpl;   // 我发的信息 模板
    QString m_fileLHtmlTmpl;  // 别人发的文件 模板
    QString m_fileRHtmlTmpl;  // 我发的文件 模板

private:
};

class MsgWebPage : public QWebEnginePage {
    Q_OBJECT;

public:
    MsgWebPage(QObject* parent = nullptr);

protected:
    bool acceptNavigationRequest(const QUrl& url, NavigationType type, bool isMainFrame);
};

/// <summary>
/// QWebEngineView用于网页内容的显示
/// </summary>
class MsgWebView : public QWebEngineView {
    Q_OBJECT

public:
    MsgWebView(QWidget* parent);
    ~MsgWebView();

public:
    void appendMsg(const QString& msgText);
    // void appendFile(const packet::file::FileInfo& info);
    void recvMsg(const QString& msgText);
    // void recvFile(const packet::file::FileInfo& info);
    void setUserInfo(const TalkUserInfo& info);

private:
    QList<QStringList> parseHtml(const QString& html);  // 解析html

    // Qt中所有Dom节点（属性、说明、文本等）都可以用QDomNode进行表示
    QList<QStringList> parseDocNode(const QDomNode& node);  // 解析节点

private:
    MsgHtmlObj*  msgHtmlObj;
    TalkUserInfo talkuserinfo;
};

#endif  // MSG_WEB_VIEW_H__
